package com.sp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sp.model.dto.UserDTO;
import com.sp.model.entity.PageResult;
import com.sp.model.entity.Result;
import com.sp.model.entity.User;
import com.sp.model.vo.UserVO;
import com.sp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sp.constant.UserMessageConstant.*;

/**
 * @author tong
 */
@Api(tags = "用户管理api接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("新增客户信息")
    @PostMapping
    public Result<Object> addUser(
            @ApiParam(name = "userDTO", value = "用户信息参数") @RequestBody UserDTO userDTO) {
        if (userService.addUser(userDTO)) {
            return Result.success(ADD_USER_SUCCESS);
        }
        return Result.error(ADD_USER_FAIL);
    }

    @ApiOperation("批量删除客户信息")
    @DeleteMapping("/{ids}")
    public Result<Object> deleteUser(
            @ApiParam(name = "ids", value = "用户id列表") @PathVariable("ids") List<Integer> ids) {
        if (userService.deleteUsers(ids)) {
            return Result.success(DELETE_USER_SUCCESS);
        }
        return Result.error(DELETE_USER_FAIL);
    }

    @ApiOperation("修改客户信息")
    @PutMapping
    public Result<Object> updateUser(
            @ApiParam(name = "userDTO", value = "用户信息参数") @RequestBody UserDTO userDTO) {
        if (userService.updateUser(userDTO)) {
            return Result.success(UPDATE_USER_FAIL);
        }
        return Result.error(UPDATE_USER_FAIL);
    }

    @ApiOperation("根据id查询客户信息")
    @GetMapping("/{id}")
    public Result<UserVO> selectById(
            @ApiParam(name = "id", value = "用户id") @PathVariable("id") Integer id) {
        User user = userService.selectById(id);
        if (user != null) {
            return Result.success(SELECT_USER_SUCCESS, BeanUtil.copyProperties(user, UserVO.class));
        }
        return Result.error(SELECT_USER_FAIL);
    }

    @ApiOperation("分页查询")
    @GetMapping("/pages")
    public Result<PageResult<UserVO>> pages(@ApiParam(name = "currentPage", value = "页码") Integer currentPage,
                                            @ApiParam(name = "pageSize", value = "每页条数") Integer pageSize,
                                            @ApiParam(name = "userDTO", value = "用户信息参数") UserDTO userDTO) {
        IPage<User> pages = userService.page(currentPage, pageSize, userDTO);
        if (pages != null) {
            List<UserVO> userVOS = BeanUtil.copyToList(pages.getRecords(), UserVO.class);

            return Result.success(SELECT_USER_SUCCESS,
                    new PageResult<>(pages.getSize(), pages.getTotal(), pages.getCurrent(), pages.getPages(), userVOS));
        }
        return Result.error(SELECT_USER_FAIL);
    }

}
